import jsonData from './data.json';


export default {
	post: function(url, data,loading=true) {
		return new Promise((succ, error) => {
			this.ajax({
				url: url,
				method: "post",
				data: data,
				loading:loading,
				success: function(e){succ.call(this, e)}, 
				fail: function(e){error?error.call(this, e):''}
			})
		})
	},
	get: function(url, data,loading=true) {
		return new Promise((succ, error) => {
			this.ajax({
				url: url,
				method: "GET",
				data: data,
				loading:loading,
				success: function(e){succ.call(this, e)
				}, 
				fail: function(e){error?error.call(this, e):''}
			})
		})
	},
	ajax: function({ url, method, data, loading, success, fail, complete, }) {
		//success(jsonData[url]);
		//return;
		let baseUrl = "http://rap2api.taobao.org/app/mock/318020/api/platform";
		let _this = this;
		if(loading){uni.showLoading({mask:true,title: "加载中"});}
		uni.request({
			url: baseUrl + url,
			data: data,
			method: method,
			header: {
				"Accept":'application/json'
			},
			success: function(e) {
				//console.log(e)
				if(e!=null && e.statusCode!=200){
					_this.errorStatusCode(e)
				}else if(e.data.success){
					console.log(e.data.data)
					success(e.data.data);
				}else if(e.data.msg){
					//延迟加载弹框，提示错误信息
					setTimeout(
						function(message){
							console.log(message);
							uni.showToast({icon:'none',title: message})
						},100,e.data.msg)
					fail(e);
				}else{
					success(e.data);
				}
			},
			fail: function(e) {
				//console.log(e)
				fail(e);
			},
			complete:function(e){
				//console.log(e)
				uni.hideLoading();
				//let req=data?JSON.stringify(data):'无';let res=JSON.stringify(e.data);
				//console.log(`请求路径：${url}\n请求方法：${method}\n请求报文：${req}\n返回报文:${res}`);
			}
		})
	},
	errorStatusCode: function(e){
		console.log(e);
		uni.showModal({
			content: e.data.msg || e.data.errMsg || '系统未知异常',
			showCancel: false
		})
	}
}


